package com.zsh.web.utils;/**
 * @Author: 003089
 * @Description: 校验文件格式
 * @Date: 2019/5/31 11:21
 */

import com.zsh.web.entity.User;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: 003089
 * @Description: 校验文件格式
 * @Date: 2019/5/31 11:21
 */
public class FileVerifyUtils {

    private static final String[] IMGES = {"JPG", "JPEG", "PNG", "GIF", "TIF", "TIFF", "PSD", "BMP"};

    private static final Map<String, String> FILE_TYPES = new HashMap<String, String>();

    static {
        // images
        FILE_TYPES.put("FFD8FF", "jpg");
        FILE_TYPES.put("FFD8FFE0", "jpg");
        FILE_TYPES.put("89504E47", "png");
        FILE_TYPES.put("47494638", "gif");
        FILE_TYPES.put("49492A00", "tif");
        FILE_TYPES.put("4D4D002A", "tif");
        FILE_TYPES.put("424D", "bmp");
        FILE_TYPES.put("424DC00C", "bmp");
        FILE_TYPES.put("38425053", "psd");
        // CAD
        FILE_TYPES.put("41433130", "dwg");
        // 日记本
        FILE_TYPES.put("7B5C727466", "rtf");
        FILE_TYPES.put("3C3F786D6C", "xml");
        FILE_TYPES.put("68746D6C3E", "html");
        // 邮件
        FILE_TYPES.put("44656C69766572792D646174653A", "eml");
        FILE_TYPES.put("D0CF11E0", "doc");
        FILE_TYPES.put("5374616E64617264204A", "mdb");
        FILE_TYPES.put("252150532D41646F6265", "ps");
        FILE_TYPES.put("255044462D312E", "pdf");
        FILE_TYPES.put("504B0304", "docx");
        FILE_TYPES.put("52617221", "rar");
        FILE_TYPES.put("57415645", "wav");
        FILE_TYPES.put("41564920", "avi");
        FILE_TYPES.put("2E524D46", "rm");
        FILE_TYPES.put("000001BA", "mpg");
        FILE_TYPES.put("000001B3", "mpg");
        FILE_TYPES.put("6D6F6F76", "mov");
        FILE_TYPES.put("3026B2758E66CF11", "asf");
        FILE_TYPES.put("4D546864", "mid");
        FILE_TYPES.put("1F8B08", "gz");
        FILE_TYPES.put("4D5A9000", "exe/dll");
        FILE_TYPES.put("75736167", "txt");
    }

    /**
     * @param file
     * @return 图片格式是否正确
     */
    public static boolean verifyImage(MultipartFile file) {
        if (null == file) {
            return false;
        }
        String fileName = file.getOriginalFilename();
        if (StringUtils.isBlank(fileName)) {
            return false;
        }
        String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
        //获取文件头信息
        String fileHeader = getFileHeader(file);
        String fileType = FILE_TYPES.get(fileHeader);
        if (StringUtils.isBlank(fileType) || StringUtils.isBlank(fileType)) {
            return false;
        }
        String upperType = fileType.toUpperCase();
        String upperSuffix = suffix.toUpperCase();
        if (upperSuffix.equals(upperType)) {
            return Arrays.asList(IMGES).contains(upperSuffix);
        }
        //当文件头和文件尾部不匹配时  判断 是否是 JPG 和 TIF
        if ("JPG".equals(upperType)) {
            upperType = "JPEG";
        }
        if ("TIF".equals(upperType)) {
            upperType = "TIFF";
        }
        return upperType.equals(upperSuffix);
    }


    public static String getFileHeader(MultipartFile file) {
        InputStream is = null;
        String value = null;
        try {
            is = file.getInputStream();
            byte[] b = new byte[4];
            is.read(b, 0, b.length);
            value = bytesToHexString(b);
        } catch (Exception e) {
        } finally {
            if (null != is) {
                try {
                    is.close();
                } catch (IOException e) {
                }
            }
        }
        return value;
    }

    private static String bytesToHexString(byte[] src) {
        StringBuilder builder = new StringBuilder();
        if (src == null || src.length <= 0) {
            return null;
        }
        String hv;
        for (int i = 0; i < src.length; i++) {
            hv = Integer.toHexString(src[i] & 0xFF).toUpperCase();
            if (hv.length() < 2) {
                builder.append(0);
            }
            builder.append(hv);
        }
        return builder.toString();
    }



}
